// 处理标签自定义key value
export function tagsObjSwitch(obj) {
  var arr = []
  if (obj) {
    for (var i in obj) {
      arr.push({
        tagsId: i,
        tagsName: obj[i]
      })
    }
  }
  return arr
}

// 添加级联组合id
export function assignment(data, childer = 'sub') {
  var selectArr
  var index = 0
   var newA =[]
  var arrMap = (arr,lenj1,pItem,pId) => {
      const atr = arr
    // newA = pItem
    arr.map(item => {
      if(lenj1){
        //pItem[pItem.length] = item.tagsId
       // Object.assign(newA,pItem)
         item['selectArr'] =  pItem
         item['selectArr'] = item['selectArr'].concat(item.tagsId)
        //console.log("pItem",pItem,item['selectArr'])
      }else{
        item['selectArr'] = [selectArr[0], item.tagsId]
      }
      var selAr = item['selectArr']
      if(item[childer] && item[childer] instanceof Array && item[childer].length > 0) arrMap(item[childer],1,selAr,item.tagsId)
      return item
    })
  }
  data.map(item => {
    selectArr = []
    selectArr.push(item.tagsId)
    item['selectArr'] = item.tagsId
    if (item[childer] && item[childer] instanceof Array && item[childer].length > 0) {
      arrMap(item[childer],null,null,item.tagsId)
    }

  })
  return data
}

// 数组降维去重
export function reduceArr(arr, childer = 'sub') {
  console.log('childer:', childer)
  const newArr = []
  for (var i in arr) {
    newArr.push(arr[i])
  }
  const toArr = (arr) => {
    arr.forEach((item) => {
      item[childer] instanceof Array ? toArr(item[childer]) : '',
      newArr.push(item)
    })
  }
  toArr(arr)
  var newArr_ = newArr.filter(
    (self, index, newArr) => index === newArr.indexOf(self)
  )
  return newArr_
}

// 获取所有父节点
export function getParents(arr, id, key, children = 'sub') {
  for (var i in arr) {
    if (arr[i][key] === id) {
      return [arr[i][key]]
    } else {
      var items = getParents(arr[i][children], id, key, children)
      if (items !== undefined) {
        return items.concat(arr[i][key])
      }
    }
  }
}

// 根据选中数组查找分类标签
export function getTagsItems(arr, option) {
  var newArr = []
  var toArr = (tagsId, optArr) => {
    optArr.map(opt => {
      if (tagsId === opt.tagsId) {
        newArr.push(opt)
      } else {
        if (opt.sub !== undefined && opt.sub.length > 0) {
          toArr(tagsId, opt.sub)
        }
      }
    })
  }
  arr.map(item => {
    var itemStr = typeof item !== 'string' ? item[item.length - 1] : item
    toArr(itemStr, option)
  })
  return newArr
}

// 组合接口数据
export function compose(arr, arrObj) {
  var str = ''
  var objStr = ''
  var _newArr = []
  arr.map(item => {
    str = typeof item === 'object' ? item.join(',') : item
    arrObj.map(objItem => {
      objStr = typeof objItem.selectArr === 'object' ? objItem.selectArr.join(',') : objItem.selectArr
      if (str === objStr) {
        _newArr.push(objItem)
      }
    })
  })
  return _newArr
}
